home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 519 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Conflicting purposes of exception spec
  5. Date: 18 Feb 1996 17:07:08 GMT
  6. Organization: Sun Microsystems Inc., Mountain View, CA
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4g7m55$pof@engnews1.Eng.Sun.COM>
  9. References: <3125C082.2946@ix.netcom.com>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Type: text
  12. X-Nntp-Posting-Host: taumet.eng.sun.com
  13. X-Newsreader: NN version 6.5.0 #21 (NOV)
  14. Content-Length: 2131
  15. Originator: clamage@taumet
  16.  
  17. "Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
  18.  
  19.  
  20. >So consider a simple function that I _know_ won't throw an exception:
  21.  
  22. >    int sumsq(int a, int b) { return a * a + b * b; }
  23.  
  24. >Do I put a throw() clause on it or not? If the compiler uses throw() 
  25. >only as an optimization hint to the caller, then I most definitely 
  26. >_do_ want to use it. If, on the other hand, the compiler treats 
  27. >throw() as a command to add code to prevent any exceptions from 
  28. >propagating out of the function, then I most definitely _don't_ want 
  29. >to use it. If I guess wrong about the interpretation, I've bought 
  30. >myself nothing, and cost myself something. And unless it's spelled out 
  31. >in the language definition, I can't make _either_ choice without 
  32. >knowing that it won't ever be compiled with a compiler that was 
  33. >designed with the opposite choice in mind.
  34.  
  35. You probably don't need to worry about bad effects of adding empty
  36. exception-specifications.
  37.  
  38. If sumsq doesn't have an exception-specification, the compiler must
  39. assume that any call to sumsq could result in any exception being
  40. thrown. If sumsq has a throw() clause, the compiler can assume that
  41. no exceptions will escape from sumsq, and perhaps can avoid adding
  42. exception-handling code to the calling location. At the call
  43. site, there are no disadvantages to sumsq's empty exception-spec,
  44. and there are potential advantages, as you noted in your article.
  45. (A function that calls sumsq might also call other functions that
  46. can throw exceptions, in which case you probably get no benefit at
  47. that call site.)
  48.  
  49. If a function has an empty exception-spec, the compiler must in
  50. general add scaffolding to the function to ensure that any exceptions
  51. occurring in the function do not escape. In the case of sumsq, the
  52. compiler may be able to determine that no exception is possible,
  53. and not generate the extra code. If the extra code is generated,
  54. on most implementations it does not add to the run time of the
  55. function.  Typically none of the extra code is executed unless an
  56. exception actually occurs and would otherwise escape from the function.
  57.  
  58. --
  59. Steve Clamage, stephen.clamage@eng.sun.com
  60.  
  61. [ To submit articles: Try just posting with your newsreader.  If that fails,
  62.               use mailto:std-c++@ncar.ucar.edu
  63.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  64.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  65.   Comments? mailto:std-c++-request@ncar.ucar.edu
  66. ]
  67.